package com.yuexunit.finance.member.action.v1;


import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.ss.usermodel.Workbook;

import com.google.inject.Singleton;
import com.yuexunit.core.BusinessException;
import com.yuexunit.finance.member.action.v1.model.EnterpriseDetail;
import com.yuexunit.finance.member.action.v1.model.MonthEarningDetailInfo;
import com.yuexunit.inject.ComponentFactory;
import com.yuexunit.util.DateUtil;
import com.yuexunit.util.StringUtil;
import com.yuexunit.web.annotation.ActionServlet;
import com.yuexunit.web.annotation.Permission;

import net.sf.jxls.transformer.XLSTransformer;

/**
 *@version 创建时间：2017年6月27日 下午2:29:56
 *@author wenwenlong
 *类说明
 */

@ActionServlet({"/api/[^/]+/exportInterestNotice.json"})
@Singleton
@Permission
public class ExportInterestNotice extends HttpServlet{

	private static final long serialVersionUID = 1L;

	private MonthEarningDetailApi monthEarningDetailApi = ComponentFactory.loadClass(MonthEarningDetailApi.class);

	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response){
		try {
			String year = request.getParameter("year");
			String month = request.getParameter("month");
			String productType = request.getParameter("productType");
			String enterpriseId = request.getParameter("enterpriseId");
			
			Integer y = null;
			Integer m = null;
			Integer p = null;
			Long e = null;
			if (StringUtil.isNotBlank(year)) {
				y = Integer.valueOf(year);
			}
			if (StringUtil.isNotBlank(month)) {
				m = Integer.valueOf(month);
			}
			if (StringUtil.isNotBlank(productType)) {
				p = Integer.valueOf(productType);
			}
			if (StringUtil.isNotBlank(enterpriseId)) {
				e = Long.valueOf(enterpriseId);
			}

			List<MonthEarningDetailInfo> megis = monthEarningDetailApi.inquiryMonthEarningDetailAllList(y, m, null, p, e);
			
			if (megis == null || megis.size() == 0) {
				throw new BusinessException("暂无数据！");
			}
			
			Map<String,Object> beans = new HashMap<String,Object>();  
			beans.put("arr", megis);
			EnterpriseActionApi enterpriseActionApi = ComponentFactory.loadClass(EnterpriseActionApi.class);
			EnterpriseDetail enterprise = enterpriseActionApi.inquireEnterpriseDetail(e);
			beans.put("enterpriseName", enterprise.getEnterpriseName());
			beans.put("year", year);
			beans.put("month", month);
			
			if (p == 2) {
				beans.put("accountName", "一润供应链管理（上海）有限公司");
				beans.put("account", "11015030684003");
				beans.put("bank", "平安银行上海自贸试验区分行营业部");
			} else {
				beans.put("accountName", "中航信托股份有限公司");
				beans.put("account", "791904313810561");
				beans.put("bank", "招商银行南昌分行营业部");
			}
			XLSTransformer transformer = new XLSTransformer();  

			InputStream in=null;  
			OutputStream out=null;  
			//设置响应  
			response.setHeader("Content-Disposition", "attachment;filename=" + new String(("收息通知单_"+year+""+month+"_"+enterprise.getEnterpriseName()+"_"+DateUtil.todayInt()+".xls").getBytes("GB2312"), "ISO_8859_1"));  
			response.setContentType("application/vnd.ms-excel");

			in = new BufferedInputStream(new FileInputStream("C:\\Finance\\upload\\update\\interestNotice.xls"));  
			Workbook workbook = transformer.transformXLS(in, beans);
			out=response.getOutputStream();  
			workbook.write(out);  
			out.flush();
			if (in != null) {
				in.close();
			}
			if (out != null) {
				out.close();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) {
		this.doGet(request, response);
	}

}
